package com.bw.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.exam.pojo.ItemCat;
import com.bw.exam.service.ItemCatService;
import com.bw.exam.mapper.ItemCatMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author liuzhiping
* @description 针对表【tb_item_cat(商品类目)】的数据库操作Service实现
* @createDate 2024-07-25 11:42:44
*/
@Service
public class ItemCatServiceImpl extends ServiceImpl<ItemCatMapper, ItemCat>
    implements ItemCatService{


    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public List<ItemCat> listTree1() {
        List<ItemCat> itemCatList = itemCatMapper.selectList(null);
        List<ItemCat> listOne = setChild((long)0,itemCatList);
        return listOne;
    }

    @Override
    public void delById(Long id) {
        LambdaQueryWrapper<ItemCat> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ItemCat::getParentId,id);
        List<ItemCat> list = this.itemCatMapper.selectList(queryWrapper);
        for(ItemCat itemCat:list){
            delById(itemCat.getId());
        }
        //删除id
        this.itemCatMapper.deleteById(id);
    }

    /***
     * 获取id
     * @param id
     * @param itemCatList
     * @return
     */
    private List<ItemCat> setChild(Long id,List<ItemCat> itemCatList){
        //获取二级
        List<ItemCat> listTwo = itemCatList.stream().filter(itemCat -> itemCat.getParentId().intValue() == id.intValue()).collect(Collectors.toList());
        //获取三级
        listTwo.stream().map(itemCat -> {
            itemCat.setChildren(setChild(itemCat.getId(),itemCatList));
            return itemCat;
        }).collect(Collectors.toList());
        return listTwo;
    }
}




